//
//  OptionalchainVC.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/27.
//  Copyright © 2017年 毕小强. All rights reserved.
//
//  可选链

import UIKit

class OptionalchainVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        let person = Person()
        print(person.residenve?.numberOfRooms ?? 0)
        person.residenve = Residence()
        print(person.residenve!.numberOfRooms)
        
        func createAddress()->Address{
            print("Function was called")
            let someAddress = Address()
            someAddress.buildingName = "American"
            someAddress.buildingNumber = "26"
            someAddress.street = "Acacia Road"
            return someAddress
        }
        
        let john = Person()
        print(john.residenve?.numberOfRooms ?? 0)
        john.residenve?.address = createAddress()
        
        //通过可选链调用方法
        if john.residenve?.printNumberOfRooms() != nil{
            print("It was possible to print the number of rooms.")
        }else{
            print("It was not possible to print the number of rooms.")
        }
        
        //通过可选链调用属性
        if (john.residenve?.address = createAddress()) != nil {
            print("It was possible to set the address.")
        }else{
            print("It was not possible to set the address.")
        }
        
        //通过可选链调用下标
        if let firstRommName = john.residenve?[0].name {
            print("The first room name is \(firstRommName).")
        }else{
            print("Unable to retrieve the first room name.")
        }
        john.residenve?[0] = Room(name: "Bathroom")
        
        let johnsHouse = Residence()
        johnsHouse.rooms.append(Room(name: "Living Room"))
        johnsHouse.rooms.append(Room(name: "Kitchen"))
        john.residenve = johnsHouse
        
        if let firstRoomName = john.residenve?[0].name{
             print("The first room name is \(firstRoomName).")
        }else{
            print("Unable to retrieve the first room name.")
        }
        
        var testScores = ["Dave":[86,82,94],"Bev": [79, 94, 81]]
        testScores["Dave"]?[0] = 91
        testScores["Bev"]?[0] += 1
        testScores["Brian"]? = [90,39,10]
        print(testScores)
        
        //多层可选链式调用
        if let johsStreet = john.residenve?.address?.street {
            print("John's street name is \(johsStreet).")
        }else{
            print("Unable to retrieve the address.")
        }
        let johnsAddress = Address()
        johnsAddress.buildingNumber = "111"
        johnsAddress.buildingName = "The Larches"
        johnsAddress.street = "长安大街"
        john.residenve?.address = johnsAddress
        
        if let johsStreet = john.residenve?.address?.street {
            print("John's street name is \(johsStreet).")
        } else {
            print("Unable to retrieve the address.")
        }
        
        //在方法的可选返回值上进行可选链式调用
        if let beginsWithThe = john.residenve?.address?.buildingIdentifier()?.hasPrefix("The") {
            if beginsWithThe {
                 print("John's building identifier begins with \"The\".")
            }else{
                print("John's building identifier does not begin with \"The\".")
            }
        }
        
        // Do any additional setup after loading the view.
    }
    
    class Person {
        var residenve:Residence?
    }
    
    class Residence {
        var rooms = [Room]()
        var numberOfRooms:Int{
            return rooms.count
        }
        subscript(i:Int)->Room{
            get{
                return rooms[i]
            }
            set{
                rooms[i] = newValue
            }
        }
        
        func printNumberOfRooms()  {
            print("The number of room is \(numberOfRooms)")
        }
        var address:Address?
        
    }
    
    
    class Room {
        let name:String
        init(name:String) {
            self.name = name
        }
    }
    
    class Address {
        var buildingName:String?
        var buildingNumber:String?
        var street:String?
        func buildingIdentifier() -> String? {
            if buildingName != nil {
                return buildingName
            }else if buildingNumber != nil && street != nil{
                return "The \(buildingNumber!) \(street!)"
            }else{
                return nil
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
